﻿using ReflectionStudio.Controls;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Linq;
using System.Xml.Linq;
using WPFExcelReport;
using INTEX.DataBase;
using System.Windows;
using System.Windows.Controls;
using WPFExcelReport.Helpers;
using ReflectionStudio.Core.Events;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXBL
    {
        public DataSet QueryVKK(string MANDANT, string KRS, string LFD)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dwData;
            DataRow dwVK04 = null; 
            try
            { 
                dwData = INTEXDB.ICC_QUERYTB.NewRow();
                dwData["VKKMANDANT"] = MANDANT;
                dwData["VKKAUFKRS"] = KRS;
                dwData["VKKAUFLFD"] = LFD;
                SResult = INTEXDBC.QueryVKKOPF(dwData, 1);
                if (SResult.Tables.Count == 0)
                    return SReturn;
                SReturn.Merge(SResult);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    dwVK04 = row;
                }
                if (dwVK04 == null)
                    return SReturn; 

               

                dwData["TXTIMANDANT"] = MANDANT;
                dwData["TXTITXTART"] = "VKT";
                dwData["TXTISIND"] = KRS + LFD;
                SResult = INTEXDBC.QueryTXTKON(dwData, 2);
                SReturn.Merge(SResult);

                SResult = Helper.ConvertTXT(SResult.Tables["TXTKON"], INTEXDB.ICC_TXTKON.NewRow());
                SReturn.Merge(SResult);

                dwData["KDMANDANT"] = dwVK04["VKKMANDANT"];
                dwData["KDKUNDNR"] = dwVK04["VKKKUNDNR"];
                SResult = INTEXDBC.QueryKDSTAMM(dwData, 1);
                SReturn.Merge(SResult);

                dwData["VKAMANDANT"] = dwVK04["VKKMANDANT"];
                dwData["VKAAUFKRS"] = dwVK04["VKKAUFKRS"];
                dwData["VKAAUFLFD"] = dwVK04["VKKAUFLFD"];
                SResult = INTEXDBC.QueryVKADR(dwData, 1);
                SReturn.Merge(SResult);
         
                SResult = INTEXDBC.QueryKDANSP(dwData, 3);
                SReturn.Merge(SResult);
                
                dwData["GLOMANDANT"] = dwVK04["VKKMANDANT"];
                dwData["GLOITEM"] = dwVK04["VKKOPITEM"];
                SResult = INTEXDBC.QueryGLOPITEM(dwData, 1);
                SReturn.Merge(SResult);

                dwData["ZABMANDANT"] = dwVK04["VKKMANDANT"];
                dwData["ZABNR"] = dwVK04["VKKZAHLBED"];
                SResult = INTEXDBC.QueryZABED(dwData, 1);
                SReturn.Merge(SResult);

                dwData["TXTIMANDANT"] = dwVK04["VKKMANDANT"];
                dwData["TXTITXTART"] = "ZAB";
                dwData["TXTISIND"] = Helper.Left(dwVK04["VKKZAHLBED"].ToString() + "V                                                                                       ", 60);
                SResult = INTEXDBC.QueryTXTKON(dwData, 1);
                SReturn.Merge(SResult);
                SResult = Helper.ConvertTXT(SResult.Tables["TXTKON"], INTEXDB.ICC_TXTKON.NewRow());
                SReturn.Merge(SResult);

                dwData["TXTIMANDANT"] = dwVK04["VKKMANDANT"];
                dwData["TXTITXTART"] = "ZAB";
                dwData["TXTISIND"] = Helper.Left(dwVK04["VKKZAHLBED"].ToString() + "E                                                                                       ", 60);
                SResult = INTEXDBC.QueryTXTKON(dwData, 1);
                SReturn.Merge(SResult);

                SResult = Helper.ConvertTXT(SResult.Tables["TXTKON"], INTEXDB.ICC_TXTKON.NewRow());
                SReturn.Merge(SResult);

                dwData["VKPMANDANT"] = dwVK04["VKKMANDANT"];
                dwData["VKPAUFKRS"] = dwVK04["VKKAUFKRS"];
                dwData["VKPAUFLFD"] = dwVK04["VKKAUFLFD"];
                SResult = INTEXDBC.QueryVKZUAB(dwData, 1);
                SReturn.Merge(SResult); 
                SResult = INTEXDBC.QueryVKPOS(dwData, 3);
                SReturn.Merge(SResult);
                if (SResult.Tables[0].Rows.Count == 0)
                    return SReturn;
                DataRow dwVKP = SResult.Tables[0].Rows[0];
                SResult = INTEXDBC.QueryVAART(dwVKP, 3);
                SReturn.Merge(SResult);
                SResult = INTEXDBC.QueryVAFARBEN(dwVKP, 3);
                SReturn.Merge(SResult);
                SResult = INTEXDBC.QueryVAKDBEZ(dwVKP, 3);
                SReturn.Merge(SResult);
                SResult = INTEXDBC.QueryTXTKON(dwVKP, 3);
                SReturn.Merge(SResult);

                SResult = Helper.ConvertTXT(SResult.Tables["TXTKON"], INTEXDB.ICC_TXTKON.NewRow());
                SReturn.Merge(SResult); 

                dwData["KDMANDANT"] = dwVK04["VKKMANDANT"];
                dwData["KDKUNDNR"] = dwVK04["VKKKUNDNR"];
                SReturn.Merge(INTEXDBC.QueryKDSTAMM(dwData, 1));

                dwData["KDAKUNDNR"] = dwVK04["VKKKUNDNR"];
                SReturn.Merge(INTEXDBC.QueryKUNDADR(dwData, 1)); 
                dwData["HRMANDANT"] = dwVK04["VKKMANDANT"];
                dwData["HRBENID"] = dwVK04["VKKSACHBE"];
                SReturn.Merge(INTEXDBC.QueryHRSTAMM(dwData, 2));
                dwData["HRBENID"] = dwVK04["VKKNEUBEN"];
                SReturn.Merge(INTEXDBC.QueryHRSTAMM(dwData, 2));
                dwData["MANDANT"] = dwVK04["VKKMANDANT"];
                dwData["KRS"] = dwVK04["VKKAUFKRS"];
                dwData["LFD"] = dwVK04["VKKAUFLFD"];
                SReturn.Merge(INTEXDBC.QueryICC_WFEVENTS(dwData,4));

            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            return SReturn;
        }
    }
}